app.controller('examPaperManageGeneration', ['$scope','$modal','$sessionStorage','$http','$rootScope','$sce','$state', function($scope,$modal,$sessionStorage,$http,$rootScope,$sce,$state){
	// 获取题目
	$scope.getPaperExer=function(){
		// 获取题型分值
		$http({
		    url:$rootScope.baseUrl+"ExamPaper/getPaperScore",
		    method:"post",
		    data:{
		    	paper_id:$sessionStorage.examID
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.scoreList=res.data.data;

		    	$http({
		    	    url:$rootScope.baseUrl+"ExamPaper/getPaperExer",
		    	    method:"post",
		    	    data:{
		    	    	paper_id:$sessionStorage.examID
		    	    }
		    	}).then(function(res){
		    	    if(res.data.code==200){
		    	    	$scope.paperExer=res.data.data;

		    	    	$scope.type=[];
		    	    	$scope.topicList={
		    	    		1:[],
		    	    		2:[],
		    	    		3:[]
		    	    	};
		    	    	$.each($scope.paperExer,function(index,item){
		    	    		if($scope.type.indexOf(item.exer_type)==-1){
		    	    			$scope.type.push(item.exer_type);
		    	    		}
		    	    		var type=$scope.topicType[item.exer_type-1];
		    	    		item.type=type;
		    	    		$scope.topicList[item.exer_type].push(item);
		    	    	});

		    	    	$scope.topicListShow={}
		    	    	$.each($scope.topicList,function(key,val){
		    	    		if(val.length!=0){
		    	    			$scope.topicListShow[key]={}
		    	    			$scope.topicListShow[key]["list"]=val;
		    	    			$scope.topicListShow[key]["type"]=val[0].type;

		    	    			$.each($scope.topicListShow[key]["list"],function(index,item){
		    	    				var arr=item.title.split('<p><img alt=');

		    	    				if(arr.length==2){
		    	    					item.title=arr.join('<p style="display:block;padding-left:24px;"><img alt=')
		    	    				}
		    	    				item.title=$sce.trustAsHtml(item.title);
		    	    				$.each(item.opt,function(optIndex,option){
		    	    					option.option_val=$sce.trustAsHtml(option.option_val);
		    	    				});
		    	    			});

		    	    			$.each($scope.scoreList,function(index,item){
		    	    				if($scope.topicListShow[item.type]){
		    	    					$scope.topicListShow[item.type]["score"]=parseFloat(item.score);
		    	    				}
		    	    			});
		    	    		}
		    	    	});

		    	    	if($scope.type.length==0){
		    	    		$scope.noData=true;
		    	    	}else {
		    	    		$scope.noData=false;
		    	    	}
		    	    }
		    	},function(error){
		    	    console.log(error);
		    	});
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getPaperExer();

	// 添加题型
	$scope.addTopicType=function(){
		var index=$scope.type.indexOf($scope.nowTopicType.id);
		if(index==-1){
			$scope.type.push($scope.nowTopicType.id);

			$scope.topicListShow[$scope.nowTopicType.id]={}
			$scope.topicListShow[$scope.nowTopicType.id]["list"]=[];
			$scope.topicListShow[$scope.nowTopicType.id]["type"]=$scope.nowTopicType;
		}else {
			sweetAlert("该题型已经添加", "不需要再添加", "error");
		}

		$scope.noData=false;
	}

	// 试卷类型
	$scope.topicType=[
		{
			"name":"单选题",
			"id":"1"
		},
		{
			"name":"多选题",
			"id":"2"
		},
		{
			"name":"判断题",
			"id":"3"
		}
	]
	$scope.nowTopicType=$scope.topicType[0];
	$scope.selectTopicType=function(item){
		$scope.nowTopicType=item;
	}

	// 删除小题
	$scope.delTopic=function(item){
		swal({
		    title: "确定删除该小题？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
			$http({
			    url:$rootScope.baseUrl+"ExamPaper/delPaperExer",
			    method:"post",
			    data:{
			    	paper_id:$sessionStorage.examID,
			    	exer_id:item.exercises_id
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("删除成功", "", "success");
			    	$scope.getPaperExer();
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		});
	}

	// 删除整个题型
	$scope.delAllType=function(item){
		swal({
		    title: "确定删除该题型？",
		    type: "warning",
		    showCancelButton: true,
		    confirmButtonColor: "#619ce1",
		    confirmButtonText: "确定",
		    closeOnConfirm: false
		},
		function() {
			$http({
			    url:$rootScope.baseUrl+"ExamPaper/delPaperExerType",
			    method:"post",
			    data:{
			    	paper_id:$sessionStorage.examID,
			    	exer_type:item.type.id
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("删除成功", "", "success");
			    	$scope.getPaperExer();
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		});
	}

	// 添加试题
	$scope.addTopic=function(type){
		$rootScope.addTopicType=type;
		var modalInstance = $modal.open({
		    templateUrl : 'addTopic.html',  //指向创建的视图
		    controller : 'addTopic',// 初始化模态范围
		    size : "lg",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {
			$scope.getPaperExer();
		}, function() {
			$scope.getPaperExer();
		});
	}

	// 向下移动
	$scope.moveDown=function(id,e){
		var $next=$(e.target).closest(".topic").next().find(".move");
		if($next.length!=0){
			// 有下一小题
			$http({
			    url:$rootScope.baseUrl+"ExamPaper/changeExerSort",
			    method:"post",
			    data:{
			    	paper_id:$sessionStorage.examID,
			    	exer_id_one:id,
			    	exer_id_two:$next.find("img").attr("ng-data")
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("调整位置成功", "", "success");
			    	$scope.getPaperExer();
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}else{
			swal("当前小题是最后一题");
		}
	}

	// 向上移动
	$scope.moveUp=function(id,e){
		var $prev=$(e.target).closest(".topic").prev().find(".move");
		if($prev.length!=0){
			// 有上一小题
			$http({
			    url:$rootScope.baseUrl+"ExamPaper/changeExerSort",
			    method:"post",
			    data:{
			    	paper_id:$sessionStorage.examID,
			    	exer_id_one:id,
			    	exer_id_two:$prev.find("img").attr("ng-data")
			    }
			}).then(function(res){
			    if(res.data.code==200){
			    	swal("调整位置成功", "", "success");
			    	$scope.getPaperExer();
			    }else if(res.data.code==409){
			    	$state.go("access.signin");
			    }
			},function(error){
			    console.log(error);
			});
		}else{
			swal("当前小题是第一题");
		}
	}
}]);

// 添加试题
app.controller('addTopic', ['$scope','$modal','$sessionStorage','$http','$rootScope','$sce','$modalInstance', '$state',function($scope,$modal,$sessionStorage,$http,$rootScope,$sce,$modalInstance,$state){
	$scope.addTopicType=$rootScope.addTopicType;

	$http({
	    url:$rootScope.baseUrl+"Exercises/getSelectList",
	    method:"post",
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.relateList=res.data.data;
	    	$scope.chapterArr=$scope.relateList.chapterArr[1];
	    	$scope.themeArr=$scope.relateList.themeArr[1];

	    	$scope.getList();
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});

	$scope.selectThemeArr=function(item){
		$scope.themeArr=item;
		$scope.getList();
	}
	$scope.selectChapterArr=function(item){
		$scope.chapterArr=item;
		$scope.getList();
	}
	
	$scope.getList=function(){
		var data={
	    	page:1,
	    	page_size:15,
	    	type:$scope.addTopicType.id,
	    	theme_id:$scope.themeArr.id,
	    	chapter_id:$scope.chapterArr.id
	    }

		$http({
		    url:$rootScope.baseUrl+"Exercises/getList",
		    method:"get",
		    params:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.listTopic=res.data.data.list;

		    	$scope.isCheck();

		    	if($scope.listTopic.length==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}

		    	// 显示分页
		    	var total=res.data.data.total;
		    	createPage(15,5,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                method:"post",
		    	                url:$rootScope.baseUrl+'Exercises/getList',
		    	                data:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	if($scope.listTopic.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;

		    	                		$scope.isCheck();
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}

	$scope.isCheck=function(){
		$.each($scope.listTopic,function(index,item){
			item.title=$sce.trustAsHtml(item.title);

			var i=$scope.exer_ids.indexOf(item.id);
			if(i==-1){
				item.isCheck=false;
			}else {
				item.isCheck=true;
			}
		});
	}

	// 关闭添加试题
	$scope.cancel=function(){
		$modalInstance.close();
	}

	// 选择试题
	$scope.exer_ids=[];
	$scope.selectTopic=function(e){
		var $checked=$(e.target).closest("tr").find("input");
		var isCheck=$checked.prop("checked");

		$checked.prop("checked",!isCheck);

		var $allChecked=$("tbody").find(":checkbox:checked");
		$.each($allChecked,function(index,ele){
			var val=$(ele).closest("td").next().html().trim();
			var i=$scope.exer_ids.indexOf(val);

			if(i==-1){
				$scope.exer_ids.push(val);
			}
		});
	}

	// 保存
	$scope.save=function(){
		$http({
		    url:$rootScope.baseUrl+"ExamPaper/addPaperExer",
		    method:"post",
		    data:{
		    	paper_id:$sessionStorage.examID,
		    	exer_ids:$scope.exer_ids,
		    	exer_type:$scope.addTopicType.id
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	swal("添加成功", "", "success");
		    	$modalInstance.close();
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
}]);

// 添加题型
app.controller('topic', ['$scope','$modal','$sessionStorage','$http','$rootScope',"$timeout",'$state',function($scope,$modal,$sessionStorage,$http,$rootScope,$timeout,$state){
	// 设置分值
	var $setPaperScore=$("#setPaperScore");
	$timeout(function(){
		$scope.addTopicType=JSON.parse($setPaperScore.attr("ng-data"));
	},30);
	$scope.changeScore=function(type,score){
		$http({
		    url:$rootScope.baseUrl+"ExamPaper/setPaperScore",
		    method:"post",
		    data:{
		    	paper_id:$sessionStorage.examID,
		    	type:type.id,
		    	score:score
		    }
		}).then(function(res){
		    if(res.data.code==200){
		    	
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
}]);
